
############## PREAMBLE ##############FOLDUP
# makefile generated 
# numas figures makefile 
#
# Created: 2009.12.30
# Copyright: Steven E. Pav, 2009-2009
# Email: shabbychef@gmail.com
# $Id$
#UNFOLD

############### FLAGS ################FOLDUP

# these don't work at times
# in that case set these by hand?

GNUPLOT     ?= $(shell which gnuplot)
OCTAVE      ?= $(shell which octave)

#dot
DOT_FLAGS    = 

AUTO_GNUPLOT_FIGS = lagrange.eps lpoly.eps spline1.eps splinterp.eps bsp0.eps bsp1.eps bsp2.eps \
							 hdip.eps badbasis.eps badpoly.eps cheby.eps eulerep.eps \
							 lsqrlog.eps fakerror.eps epos.eps eneg.eps gofy.eps beul.eps

AUTO_OCTAVE_FIGS  = dirf.eps lsk1.eps lsk2.eps cseig.eps justy.ps xandy.ps justw.ps yandw.ps \
										myf.eps rkm2.ps growth.eps growtheuler.eps growthrk.eps \
										rungecheb17.ps rungechebsm.ps runge15.ps runge50.ps rungesmall.ps 

QPS_FILES     = $(wildcard figs/*.qps)
QPS_IMP_FIGS  = $(patsubst %.qps,%.eps,$(QPS_FILES)) 
#UNFOLD

############## DEFAULT ###############FOLDUP

default : all
#UNFOLD

############## MARKERS ###############FOLDUP

.PHONY   : 
.SUFFIXES: .dat .eps .gpt .m .ps .qps .seg
.PRECIOUS: %.qps %.seg %.dat %.m
#UNFOLD

############ BUILD RULES #############FOLDUP

# qt3 kseg ps files have unicode text.
# this is nogood for psfrag texthooks.
# change them here.

%.pps : %.qps
		@echo perl func to convert $< to hookable $@;
		@$(PERL) -n -e 'BEGIN { @FE=();$$HASF=0; } \
		if (m{^.*ENC-.*\[}) { \
			print;$$esc = 0;$$HASF ||= 1; \
			while (($$line = <>) && not $$esc) \
			{ print $$line;chomp($$line); \
				while ($$line =~ s{/([^\/\]]+)}{}) {push (@FE,$$1);} \
				$$esc = ($$line =~ m{\]\s*def\s*$$});} \
			%mymaps = qw( one 1 two 2 three 3 four 4 five 5 \
			  six 6 seven 7 eight 8 nine 9 zero 0 \
        period . equal = plus + );$$mymaps{space}=q[ ]; \
			while (($$k,$$v)=each(%mymaps)) { map {s/^$$k$$/$$v/;} @FE; } \
			print qq[/myT{gsave PCol SC ty MT 1 index dup length exch stringwidth pop 1 -1 roll -1 1 scale 180 rotate exch sub exch div exch 0 exch ashow grestore}D\n]; \
		} elsif ((m{(\d{1,3}\s+Y)?<([0-9A-F]+)>(-?\d+)\s+(-?\d+)\s+A?T\s*$$}) && $$HASF) \
		{ print qq[/Times-Roman findfont 18 scalefont setfont\n]; \
			$$cod = $$2;$$str = q[]; \
			while ($$cod) { $$ch = substr($$cod,0,4,q[]); \
										  $$str .= (hex($$ch))? $$FE[hex($$ch)]:q[ ]; } \
			print qq[$$1($$str)$$3 $$4 myT\n]; \
		} else { print $$_; }' < $< > $@;

# define a rule to find a bounding box

define findbb
@echo find real bounding box for $<;
@echo -n '   was: ';
@if grep Bounding $<; then\
$(PERL) -p -e 'BEGIN { $$spit = 0;\
$$bb = qx[gs -q -dNOPAUSE -dSAFER -dDELAYSAFER -dBATCH -sDEVICE=bbox -sOutputFile=/dev/null $< 2>&1 | awk "/\%\%Bounding/ {print}"];} \
$$spit ||= (/^%%Bounding/ and $$_ = qq[$$bb]);' $< > $@;\
else\
$(PERL) -p -e 'BEGIN { $$spit = 0;\
$$bb = qx[gs -q -dNOPAUSE -dSAFER -dDELAYSAFER -dBATCH -sDEVICE=bbox -sOutputFile=/dev/null $< 2>&1 | awk "/\%\%Bounding/ {print}"];} \
$$spit ||= ($$_ = qq[$$bb$$_]);' $< > $@;\
fi
@echo -n 'is now: ';
@head $@ | grep Bounding
endef

# make correct bounding box
%.eps : %.pps 
		$(findbb)

%.eps : %.ps 
		$(findbb)

# define a rule to convert color kseg figs to black and white
define convert_to_bw
@echo converting color $< to black and white $@;
@$(PERL) -p -e 'BEGIN { sub togrey { my $$avg = int(($$_[0] + $$_[1] + $$_[2])/3.0);\
return qq[$$avg $$avg $$avg]; } }\
s/^(\S+)\s(\S+)\s(\S+)(\sP1)$$/&togrey($$1,$$2,$$3) . $$4/e;\
s/^(\S+\s)(\S+)\s(\S+)\s(\S+)(\sBR)$$/$$1 . &togrey($$2,$$3,$$4) . $$5/e;\
s/^(\S+\s\S+\s)(\S+)\s(\S+)\s(\S+)(\s\S+\s\S+\sPE)$$/$$1 . &togrey($$2,$$3,$$4) . $$5/e;' $< > $@;
endef

# make a color fig generated by kseg into a black and white...
%.bw.ps : %.ps
		$(convert_to_bw)

%.bw.eps : %.eps
		$(convert_to_bw)
#UNFOLD

############# CLEAN UP ###############FOLDUP
#UNFOLD

############## TARGETS ###############FOLDUP

#generated by gnuplot#FOLDUP
spline1.eps : figgen.gpt spline.dat
	$(GNUPLOT) figgen.gpt

splinterp.eps : figgen.gpt splinter.dat
	$(GNUPLOT) figgen.gpt

bsp0.eps : figgen.gpt bs00.dat bs01.dat bs02.dat bs03.dat
	$(GNUPLOT) figgen.gpt

hdip.eps : figgen.gpt deriv.dat
	$(GNUPLOT) figgen.gpt

eulerep.eps : figgen.gpt eulerep.dat
	$(GNUPLOT) figgen.gpt

beul.eps : figgen.gpt beul.dat
	$(GNUPLOT) figgen.gpt

lsqrlog.eps : figgen.gpt lsqr.dat
	$(GNUPLOT) figgen.gpt

$(AUTO_GNUPLOT_FIGS) : figgen.gpt
	$(GNUPLOT) figgen.gpt

#gofy.eps : backeul.gpt
#$(GNUPLOT) $^

#beul.eps : backeul.gpt beul.dat
#$(GNUPLOT) $<

#fakerror.eps : fakerror.gpt
#$(GNUPLOT) $<


#UNFOLD

#generated by octave#FOLDUP
dirf.eps : logistics.m
	$(OCTAVE) $^

lsk1.eps lsk2.eps : lsqmk.m
	$(OCTAVE) $^

cseig.eps : pca.m
	$(OCTAVE) $^

justy.ps xandy.ps justw.ps yandw.ps : plotfun.m
	$(OCTAVE) $^

myf.eps : plotvf.m
	$(OCTAVE) $^

rkm2.ps : rkm.m
	$(OCTAVE) $^

growth.eps growtheuler.eps growthrk.eps : rungrowth.m
	$(OCTAVE) $^

rungecheb17.ps rungechebsm.ps runge15.ps runge50.ps rungesmall.ps : runge.m
	$(OCTAVE) $^
#UNFOLD

#data generated by octave which then goes into gnuplot? ack!#FOLDUP
beul.dat : beul.m
	$(OCTAVE) $^

blowup.dat : blowup.m
	$(OCTAVE) $^

eulerep.dat : euler.m
	$(OCTAVE) $^

deriv.dat : darctan.m
	$(OCTAVE) $^
#UNFOLD

#UNFOLD

############### RULES ################FOLDUP

# an easy target
all: $(AUTO_GNUPLOT_FIGS) $(AUTO_OCTAVE_FIGS) $(QPS_IMP_FIGS)

clean: 
realclean: 
#UNFOLD

#for vim modeline: (do not edit)
# vim:ts=2:sw=2:tw=79:fdm=marker:fmr=FOLDUP,UNFOLD:cms=#%s:tags=tags;:syntax=make:filetype=make:ai:si:cin:nu:fo=croqt:cino=p0t0c5(0:
